@extends('layouts.common')

@section('title')认购清单@endsection

@section('content')
    <body class="easyui-layout">
        <div data-options="region:'center',title:'认购单详情'" style="padding:5px;background:#eee;">
            <div id="toolbar">
                {!! $toolBarButton !!}
            </div>
            <div id="listGrid"></div>
            <div id="form_dialog"></div>
            <input type="hidden" id="raw_id" name="raw_id" value="">
        </div>
        <div data-options="region:'south',title:'认购单产品信息',split:true" style="height:250px;">
            <div id="goodsList"></div>
        </div>
        <div id="edit-state">请确认所选认购单是否正确，然后点击操作按钮！</div>
    </body>


@endsection

@section('js')
    <script type="text/javascript">
        $(function () {
            $('#edit-state').hide();

            $('#listGrid').datagrid({
                url: '/sale/requisition/list',
                rownumbers: true,
                pagination: true,
                toolbar: '#toolbar',
                fit: true,
                singleSelect:"true",
                collapsible:false,
                fitColumns:true,
                queryParams:{
                    _token : $('meta[name="csrf-token"]').attr('content'),
                },

                columns:[[
                    { field:'id',checkbox:true},
                    { field:'sn',title:'SN',width:80,align:'center'},
                    { field:'customer_id',title:'客户ID',width:100,align:'center'},
                    { field:'customer_mobile',title:'客户手机',width:100,align:'center'},
                    { field:'customer_name',title:'客户名称',width:100,align:'center'},
                    { field:'amount',title:'总金额',width:100,align:'center'},
                    { field:'goods_num_total',title:'商品总数',width:100,align:'center'},
                    { field:'order_num_total',title:'转单总数',width:100,align:'center'},
                    { field:'doc_state',title:'审核状态',width:100,align:'center',formatter:docState},
                    { field:'warehouse_id',title:'仓库',width:100,align:'center'},
                    { field:'shipping_methods',title:'配送方式',width:100,align:'center',formatter:shipping_method},
                    { field:'shipping_address',title:'配送地址',width:100},
                    { field:'description',title:'描述',width:150,align:'center'}
                ]],
                onClickRow:function (index,raw) {
                    $("input[name='raw_id']").val(raw.id);
                    $('#goodsList').datagrid({
                        url: '/sale/requisition/goodslist?req_id='+raw.id,
                        pagination: true,
                        rownumbers: true,
                        fit: true,
                        fitColumns:true,
                        singleSelect:"true",
                        queryParams:{
                            _token : $('meta[name="csrf-token"]').attr('content'),
                        },

                        columns:[[
                            { field:'id',checkbox:true},
                            { field:'goods_id',title:'商品ID',width:80,align:'center'},
                            { field:'goods_name',title:'商品名称',width:100,align:'center'},
                            { field:'goods_price',title:'商品价格',width:100,align:'center'},
                            { field:'goods_num',title:'商品数量',width:100,align:'center'},
                            { field:'order_num',title:'转单数量',width:150,align:'center'},
                            { field:'line_amt',title:'行总价（元）',width:100 }
                        ]]
                    });
                }
            });

        });

        var docState = function (value,row,index) {
            if(value == "0") {
                return '<span class="icon-stop" style="width: 16px;height: 16px;display: inline-block;"></span>';
            }else if(value == "20"){
                return '<span class="icon-ok" style="width: 16px;height: 16px;display: inline-block;"></span>';
            }else{
                return '<span class="icon-remove" style="width: 16px;height: 16px;display: inline-block;"></span>';
            }
        };

        var shipping_method = function (value,row,index) {
            if(value == "1") {
                return '自提';
            }else{
                return '配送';
        }};

        function openAdd(){

            index = $('#J_Nav .dl-selected', window.parent.document).attr('data_code');
            parent.addTab(index, '新建认购单', '/sale/requisition/add', 'icon-add', 1);
        }

        function openEdit(){
            var raw_id = $("input[name='raw_id']").val();
            if (raw_id === '') {
                $.messager.alert('警告','请选择一行');
            }else{
                index = $('#J_Nav .dl-selected', window.parent.document).attr('data_code');
                parent.addTab(index, '编辑认购单', '/sale/requisition/edit?req_id='+raw_id, 'icon-add', 1);
            }

        }

        function audit() {

            var raw_id = $("input[name='raw_id']").val();
            if (raw_id === '') {
                $.messager.alert('警告', '请选择一行');
            } else {

                $('#edit-state').dialog({
                    title: '认购审核',
                    width: 300,
                    height: 200,
                    closed: false,
                    cache: false,
                    modal: true,
                    buttons: [{
                        text: '通过',
                        iconCls: 'icon-ok',
                        handler: function () {
                            $.ajax({
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                },
                                url: '/sale/requisition/audit',
                                type: 'post',
                                data: {
                                    req_id: raw_id,
                                    doc_state:"20"
                                },
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code === 200) {
                                        $('#edit-state').dialog('close');
                                        $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                                    } else {
                                        $.messager.alert("提示信息", data.data, "error");
                                    }
                                }
                            });
                        }
                    }, {
                        text: '不通过',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $.ajax({
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                },
                                url: '/sale/requisition/audit',
                                type: 'post',
                                data: {
                                    req_id: raw_id,
                                    doc_state:"0"
                                },
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code === 200) {
                                        $('#edit-state').dialog('close');
                                        $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                                    } else {
                                        $.messager.alert("提示信息", data.data, "error");
                                    }
                                }
                            });
                        }
                    }]
                });
            }
        }

        function save(){
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url:'/sys/user/powersave',
                type:'post',
                data:$('#info-form').serialize(),
                dataType:'json',
                success:function (data) {
                    if(data.code === 200){
                        $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                        $('#form_dialog').dialog('close');
                    }else{
                        $.messager.alert("提示信息", data.data , "error");
                    }
                }
            });
        }

        function remove(){
            var raw_id = $("input[name='raw_id']").val();
            if (raw_id === '') {
                $.messager.alert('警告','请选择一行');
            }else {
                $.messager.confirm('信息提示','确定要删除该记录？', function(result){
                    if(result){

                        $.ajax({
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            type:'post',
                            url:'/sys/user/powerremove',
                            data:{
                                id:raw_id
                            },
                            success:function(data){
                                if(data.code === 200){
                                    $.messager.alert('信息提示','删除成功！');
                                    $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                                }
                                else {
                                    $.messager.alert('信息提示','删除失败,请稍后再试！');
                                    $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                                }
                            }
                        });
                    }
                });
            }
        }

        function changeOrder() {
            var raw_id = $("input[name='raw_id']").val();
            index = $('#J_Nav .dl-selected', window.parent.document).attr('data_code');
            parent.addTab(index, '编辑订单', '/sale/order?req_id='+raw_id, 'icon-add', 1);
        }
    </script>
@endsection